/*
 * Copyright (c) 2023-2025 elsfs Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.elsfs.cloud.common.mybatis.ext.db;

import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import org.elsfs.cloud.common.util.lang.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.lang.NonNull;

/** SQL执行器初始化器 */
public class SqlExecutorsInitializer implements BeanPostProcessor {

  /**
   * Apply this {@code BeanPostProcessor} to the given new bean instance <i>after</i> any bean
   * initialization callbacks (like InitializingBean's {@code afterPropertiesSet} or a custom
   * init-method). The bean will already be populated with property values. The returned bean
   * instance may be a wrapper around the original.
   *
   * <p>In case of a FactoryBean, this callback will be invoked for both the FactoryBean instance
   * and the objects created by the FactoryBean (as of Spring 2.0). The post-processor can decide
   * whether to apply to either the FactoryBean or created objects or both through corresponding
   * {@code bean instanceof FactoryBean} checks.
   *
   * <p>This callback will also be invoked after a short-circuiting triggered by a {@link
   * InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation} method, in contrast to all
   * other {@code BeanPostProcessor} callbacks.
   *
   * <p>The default implementation returns the given {@code bean} as-is.
   *
   * @param bean the new bean instance
   * @param beanName the name of the bean
   * @return the bean instance to use, either the original or a wrapped one; if {@code null}, no
   *     subsequent BeanPostProcessors will be invoked
   * @throws BeansException in case of errors
   * @see InitializingBean#afterPropertiesSet
   * @see FactoryBean
   */
  @Override
  public Object postProcessAfterInitialization(@NonNull Object bean, @NonNull String beanName)
      throws BeansException {
    if (bean instanceof SqlExecutorFactory && StringUtils.equals(beanName, "sqlExecutorFactory")) {
      try {
        MethodHandles.privateLookupIn(SqlExecutors.class, MethodHandles.lookup())
            .findStatic(
                SqlExecutors.class,
                "setSqlExecutorFactory",
                MethodType.methodType(void.class, SqlExecutorFactory.class))
            .invoke(bean);
      } catch (Throwable e) {
        throw new BeanInitializationException("初始化SqlExecutors类失败", e);
      }
    }

    return bean;
  }
}
